/*
 * GameWindow.cpp
 *
 *  Created on: Apr 16, 2009
 *      Author: Ry
 */

#ifdef WIN32
#include <windows.h>
#endif

#include <GL/glew.h>
#include <GL/glu.h>
#include <SDL/SDL.h>
#include <iostream>

#include "GameWindow.h"
#include "exceptions.h"
#include "event/events.h"

using namespace std;

GameWindow::GameWindow() {
	mode = WINDOWED;

	// Configure default windowed resolution
	windowedResolution.width = 640;
	windowedResolution.height = 480;

	// Configure default fullscreen resolution (use desktop res by default)
	const SDL_VideoInfo *videoInfo = SDL_GetVideoInfo();
	fullScreenResolution.width = videoInfo->current_w;
	fullScreenResolution.height = videoInfo->current_h;

	window();
}

void GameWindow::setTitle(char* title) {
	SDL_WM_SetCaption(title, title);
}

char* GameWindow::getTitle() {
	char *title, *icon;
	SDL_WM_GetCaption(&title, &icon);
	return title;
}

void GameWindow::resize(int width, int height) {
	screen = SDL_SetVideoMode(width, height, 32, SDL_OPENGL | SDL_RESIZABLE);
	windowedResolution.width = width;
	windowedResolution.height = height;
}

void GameWindow::fullscreen() {
	screen = SDL_SetVideoMode(fullScreenResolution.width, fullScreenResolution.height, 32, SDL_OPENGL | SDL_FULLSCREEN);
	if (screen == NULL) {
		throw GameException(SDL_GetError());
	}
	mode = FULLSCREEN;
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = FULLSCREENED_EVENT;
	event.user.data1 = (void*)&fullScreenResolution;
	SDL_PushEvent(&event);
}

void GameWindow::window() {
	screen = SDL_SetVideoMode(windowedResolution.width, windowedResolution.height, 32, SDL_OPENGL | SDL_RESIZABLE);
	if (screen == NULL) {
		throw GameException(SDL_GetError());
	}
	mode = WINDOWED;
	SDL_Event event;
	event.type = SDL_USEREVENT;
	event.user.code = WINDOWED_EVENT;
	event.user.data1 = (void*)&windowedResolution;
	SDL_PushEvent(&event);
}

void GameWindow::toggleFullScreen() {
	switch(mode) {
	case WINDOWED:
		fullscreen();
		break;
	case FULLSCREEN:
		window();
		break;
	}
}

WindowResolution GameWindow::getCurrentResolution() {
	return mode == WINDOWED ? windowedResolution : fullScreenResolution;
}

GameWindow::~GameWindow() {}
